/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Formularios;

import Clases.ConexionMySql;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Administrador
 */
public class stock extends javax.swing.JFrame {
    private DefaultTableModel busqueda;

    /**
     * Creates new form stock
     */
    public stock() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        stock_busqueda = new javax.swing.JTextField();
        buscar_nombre = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        vista = new javax.swing.JTable();
        jButton10 = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        stock_busqueda.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                stock_busquedaActionPerformed(evt);
            }
        });

        buscar_nombre.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/buscar.png"))); // NOI18N
        buscar_nombre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buscar_nombreActionPerformed(evt);
            }
        });

        jLabel10.setText("Nombre: ");

        vista.setBackground(new java.awt.Color(204, 204, 204));
        vista.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Id Stock", "Id Producto", "Producto", "Stock", "Fecha de Ingreso"
            }
        ));
        vista.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                vistaMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(vista);

        jButton10.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/close.png"))); // NOI18N
        jButton10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton10ActionPerformed(evt);
            }
        });

        jLabel6.setFont(new java.awt.Font("Tahoma", 3, 30)); // NOI18N
        jLabel6.setText("STOCK ");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 656, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel10)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(stock_busqueda, javax.swing.GroupLayout.PREFERRED_SIZE, 228, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(29, 29, 29)
                        .addComponent(buscar_nombre, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton10, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addGap(177, 177, 177))
            .addGroup(layout.createSequentialGroup()
                .addGap(263, 263, 263)
                .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel6)
                .addGap(20, 20, 20)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel10)
                        .addComponent(stock_busqueda, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(buscar_nombre)
                        .addComponent(jButton10, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(127, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void buscar_nombreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buscar_nombreActionPerformed

        ConexionMySql con = new ConexionMySql();
        Connection cnn = con.conectar();
        String[] titulos = {"Id Stock","Id Producto","Producto","Stock","Fecha de Ingreso"};
        busqueda = new DefaultTableModel(null,titulos);
        String [] registro = new String[5];
        String id = (stock_busqueda.getText());
        String sql = "SELECT Id_Stock,Id_producto,Producto,Stock,Fecha_ingreso FROM stock where Producto LIKE '%" + id +"%'";
        try
        {
            Statement st = cnn.createStatement();
            ResultSet rs = st.executeQuery(sql);

            while(rs.next()){
                registro[0]= rs.getString(1);
                registro[1]= rs.getString(2);
                registro[2]= rs.getString(3);
                registro[3]= rs.getString(4);
                registro[4]= rs.getString(5);
                

                busqueda.addRow(registro);
            }
            vista.setModel(busqueda);
        }
        catch (SQLException e)
        {
            JOptionPane.showMessageDialog(null, e);
            llamar();
        }
    }//GEN-LAST:event_buscar_nombreActionPerformed

    private void vistaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_vistaMouseClicked
        int filasel = vista.getSelectedRow();
        String valor = (String) busqueda.getValueAt(filasel,0);
        PasarTabla(valor);
    }//GEN-LAST:event_vistaMouseClicked

    private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton10ActionPerformed
        atras();// TODO add your handling code here:
    }//GEN-LAST:event_jButton10ActionPerformed

    private void stock_busquedaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stock_busquedaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_stock_busquedaActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(stock.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(stock.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(stock.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(stock.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new stock().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buscar_nombre;
    private javax.swing.JButton jButton10;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField stock_busqueda;
    private javax.swing.JTable vista;
    // End of variables declaration//GEN-END:variables
   
    String actualizar ="";
    private void PasarTabla(String valor) {
      ConexionMySql con = new ConexionMySql();
      Connection cnn = con.conectar();
    String sql = "SELECT Id_Stock,Id_producto,Producto,Stock,Fecha_ingreso FROM stock where Producto = "+ valor;  
    try 
      {
        Statement st = cnn.createStatement();
        ResultSet rs = st.executeQuery(sql);
        if(rs.next()){
        /*codigo_producto.setText(rs.getString(1));
        nombre_producto.setText(rs.getString(2));
        cantidad_producto.setText(rs.getString(3));
        fecha_ingreso.setDate(rs.getDate(4));*/
            
            
          
        }
      } 
      catch (SQLException e) 
      {
            JOptionPane.showMessageDialog(null, e);
      }
      actualizar = valor;
    }

    private void atras() {
        
    }

    private void llamar() {
        ConexionMySql con = new ConexionMySql();
        Connection cnn = con.conectar();
        String[] titulos = {"Id Stock","Id Producto","Producto","Stock","Fecha de Ingreso"};
        busqueda = new DefaultTableModel(null,titulos);
        String [] registro = new String[5];
        String id = (stock_busqueda.getText());
        String sql = "SELECT Id_Stock,Id_producto,Producto,Stock,Fecha_ingreso FROM stock where Producto LIKE '%" + id +"%'";
        try
        {
            Statement st = cnn.createStatement();
            ResultSet rs = st.executeQuery(sql);

            while(rs.next()){
                registro[0]= rs.getString(1);
                registro[1]= rs.getString(2);
                registro[2]= rs.getString(3);
                registro[3]= rs.getString(4);
                registro[4]= rs.getString(5);
                

                busqueda.addRow(registro);
            }
            vista.setModel(busqueda);
        }
        catch (SQLException e)
        {
            JOptionPane.showMessageDialog(null, e);

        } 
    }
}
